/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 三峡人寿业务前置平台系统
 * 创建日期 : 2016-12-09
 * 修改历史 :
 * 1. [2016-12-09]创建文件 by bianzb
 */
package com.sinosoft.sinopay.service.payrules.biz.service.impl;

import com.sinosoft.sinocloud.component.sinorules.api.bean.RuleEngineBean;
import com.sinosoft.sinocloud.platform.common.exception.biz.SinoBizException;
import com.sinosoft.sinocloud.platform.common.exception.system.SinoSystemException;
import com.sinosoft.sinopay.payrules.config.ENUM_RULE_RESULT;
import com.sinosoft.sinopay.payrules.dto.MerchantCheckDTO;
import com.sinosoft.sinopay.payrules.dto.MerchantNotifyDTO;
import com.sinosoft.sinopay.payrules.dto.PayChannelCheckDTO;
import com.sinosoft.sinopay.payrules.dto.PayRouteCheckDTO;
import com.sinosoft.sinopay.service.payrules.biz.integration.dao.PayRuleMapper;
import com.sinosoft.sinopay.service.payrules.biz.integration.dao.PayWayMapper;
import com.sinosoft.sinopay.service.payrules.biz.service.PayRuleCoreService;
import com.sinosoft.sinopay.service.payrules.config.ENUM_RULE_TYPE;
import com.sinosoft.sinopay.service.payrules.dto.MerchantCheckRuleInfo;
import com.sinosoft.sinopay.service.payrules.dto.PayRouteCheckRuleInfo;
import com.sinosoft.sinopay.service.payrules.generated.mapper.*;
import com.sinosoft.sinopay.service.payrules.generated.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【支付规则业务服务接口实现类】
 *
 * @author bianzb
 * @version 1.0
 * @date 2016-12-09 11:36
 */

@Service
public class PayRuleCoreServiceImpl implements PayRuleCoreService {
    @Autowired
    private RuleEngineBean ruleEngine;
    @Autowired
    private PayRulesMerchantConfigMapper payRulesMerchantConfigMapper;
    @Autowired
    private RelaPayRulesMerchantMapper relaPayRulesMerchantMapper;
    @Autowired
    private PayRulesThirdchnlLimitMapper payRulesThirdchnlLimitMapper;
    @Autowired
    private PayRulesRouteConfigMapper payRulesRouteConfigMapper;
    @Autowired
    private PayRuleMapper payRuleMapper;


    @Override
    public void checkMerchant(MerchantCheckDTO merchantCheckDTO) throws SinoBizException, SinoSystemException {
        //查询商户支付信息配置信息
        PayRulesMerchantConfigExample payRulesMerchantConfigExample = new PayRulesMerchantConfigExample();
        payRulesMerchantConfigExample.createCriteria().andMerchantCodeEqualTo(merchantCheckDTO.getMerchantCode());
        List<PayRulesMerchantConfig> payRulesMerchantConfigList = payRulesMerchantConfigMapper.selectByExample(payRulesMerchantConfigExample);

        //初始化规则校验DTO
        MerchantCheckRuleInfo merchantCheckRuleInfo = new MerchantCheckRuleInfo();
        merchantCheckRuleInfo.setMerchantCode(merchantCheckDTO.getMerchantCode());
        merchantCheckRuleInfo.setPayAmount(merchantCheckDTO.getPayAmount());
        merchantCheckRuleInfo.setPayType(merchantCheckDTO.getPayType());
        merchantCheckRuleInfo.setMerchantRuleDefList(payRulesMerchantConfigList);

        //查询触发的规则组
        RelaPayRulesMerchantExample relaPayRulesMerchantExample = new RelaPayRulesMerchantExample();
        relaPayRulesMerchantExample.createCriteria()
                .andBusinessCodeEqualTo(merchantCheckDTO.getMerchantCode())
                .andRuleTypeEqualTo(ENUM_RULE_TYPE.MERCHAINT_CHECK.code());
        List<RelaPayRulesMerchant> relaPayRulesMerchantList= relaPayRulesMerchantMapper.selectByExample(relaPayRulesMerchantExample);
        //1、添加规则数据对象
        List<Object> ruleData = new ArrayList<Object>();
        ruleData.add(merchantCheckRuleInfo);

        //2、添加全局变量
        Map<String,Object> tGlobalMap = new HashMap<String,Object>();

        //3、触发的规则组
        List<String> tAgendaGroup = new ArrayList<String>();

        for (int i = 0; i < relaPayRulesMerchantList.size(); i++) {
            tAgendaGroup.add(relaPayRulesMerchantList.get(i).getRuleCode());
        }
        //4、执行规则引擎
        ruleEngine.execute(ruleData,tGlobalMap,tAgendaGroup);


        if(merchantCheckRuleInfo.getResultCode().equals(ENUM_RULE_RESULT.FAIL.code())){
            throw new SinoBizException(merchantCheckRuleInfo.getResultMsg());
        }
    }

    @Override
    public PayRouteCheckDTO payRoute(PayRouteCheckDTO payRouteCheckDTO) throws SinoBizException, SinoSystemException {
        //查询支付路由配置信息
        PayRulesRouteConfigExample payRulesRouteConfigExample = new PayRulesRouteConfigExample();
        payRulesRouteConfigExample.createCriteria().andPayTypeEqualTo(payRouteCheckDTO.getPayType());
        List<PayRulesRouteConfig> payRulesRouteConfigList = payRulesRouteConfigMapper.selectByExample(payRulesRouteConfigExample);

        PayRouteCheckRuleInfo payRouteCheckRuleInfo = new PayRouteCheckRuleInfo();
        payRouteCheckRuleInfo.setPayAmount(payRouteCheckDTO.getPayAmount());
        payRouteCheckRuleInfo.setPayType(payRouteCheckDTO.getPayType());
        payRouteCheckRuleInfo.setRouteConfigList(payRulesRouteConfigList);
        payRouteCheckRuleInfo.setThirdpayCode(payRulesRouteConfigList.get(0).getThirdpayCode());
        payRouteCheckRuleInfo.setThirdpayImplName(payRulesRouteConfigList.get(0).getThirdPayImplName());
        //查询触发的规则组
        RelaPayRulesMerchantExample relaPayRulesMerchantExample = new RelaPayRulesMerchantExample();
        relaPayRulesMerchantExample.createCriteria()
                .andBusinessCodeEqualTo("PAYROUTE")
                .andRuleTypeEqualTo(ENUM_RULE_TYPE.PAY_ROUTE.code());
        List<RelaPayRulesMerchant> relaPayRulesMerchantList= relaPayRulesMerchantMapper.selectByExample(relaPayRulesMerchantExample);

        //1、添加规则数据对象
        List<Object> ruleData = new ArrayList<Object>();
        ruleData.add(payRouteCheckRuleInfo);

        //2、添加全局变量
        Map<String,Object> tGlobalMap = new HashMap<String,Object>();

        //3、触发的规则组
        List<String> tAgendaGroup = new ArrayList<String>();

        for (int i = 0; i < relaPayRulesMerchantList.size(); i++) {
            tAgendaGroup.add(relaPayRulesMerchantList.get(i).getRuleCode());
        }
        //4、执行规则引擎
        ruleEngine.execute(ruleData,tGlobalMap,tAgendaGroup);

        payRouteCheckDTO.setThirdpayCode(payRouteCheckRuleInfo.getThirdpayCode());
        payRouteCheckDTO.setThirdpayImplName(payRouteCheckRuleInfo.getThirdpayImplName());

        return payRouteCheckDTO;
    }

    @Override
    public void checkPayChannel(PayChannelCheckDTO payChannelCheckDTO) throws SinoBizException, SinoSystemException {
        //查询触发的规则组
        RelaPayRulesMerchantExample relaPayRulesMerchantExample = new RelaPayRulesMerchantExample();
        relaPayRulesMerchantExample.createCriteria()
                .andBusinessCodeEqualTo(payChannelCheckDTO.getThirdpayCode())
                .andRuleTypeEqualTo(ENUM_RULE_TYPE.PAY_CHANNEL.code());
        List<RelaPayRulesMerchant> relaPayRulesMerchantList= relaPayRulesMerchantMapper.selectByExample(relaPayRulesMerchantExample);

        PayRulesThirdchnlLimitExample payRulesThirdchnlLimitExample = new PayRulesThirdchnlLimitExample();
        payRulesThirdchnlLimitExample.createCriteria()
                .andThirdpayCodeEqualTo(payChannelCheckDTO.getThirdpayCode());

        List<PayRulesThirdchnlLimit> payRulesThirdchnlLimitList = payRulesThirdchnlLimitMapper.selectByExample(payRulesThirdchnlLimitExample);

        //1、添加规则数据对象
        List<Object> ruleData = new ArrayList<Object>();
        ruleData.add(payChannelCheckDTO);

        if(payRulesThirdchnlLimitList.size() > 0){
            ruleData.add(payRulesThirdchnlLimitList.get(0));
        }

        //2、添加全局变量
        Map<String,Object> tGlobalMap = new HashMap<String,Object>();
        //3、触发的规则组
        List<String> tAgendaGroup = new ArrayList<String>();

        for (int i = 0; i < relaPayRulesMerchantList.size(); i++) {
            tAgendaGroup.add(relaPayRulesMerchantList.get(i).getRuleCode());
        }
        //4、执行规则引擎
        ruleEngine.execute(ruleData,tGlobalMap,tAgendaGroup);

        if(payChannelCheckDTO.getResultCode().equals(ENUM_RULE_RESULT.FAIL.code())){
            throw new SinoBizException(payChannelCheckDTO.getResultMsg());
        }
    }

    @Override
    public List<MerchantNotifyDTO> queryMerchantNotifyUrl(String merchantCode) throws SinoBizException, SinoSystemException {

        List<MerchantNotifyDTO> merchantNotifyDTOList = payRuleMapper.selectMerchantNotifyUrl(merchantCode);
        if(merchantNotifyDTOList == null || merchantNotifyDTOList.size() <= 0){
            throw new SinoSystemException("商户配置支付回调配置信息不存在");
        }
        return merchantNotifyDTOList;
    }
}
